/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.utp.proyectobd;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author JOSE
 */
public class Actividades extends javax.swing.JFrame {

    /**
     * Creates new form Actividades
     */
    public Actividades() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tidplanta = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        bidplanta = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        tidact = new javax.swing.JTextField();
        bidact = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtabla = new javax.swing.JTable();
        bregresar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Actividades");

        jLabel2.setText("Id Planta:");

        bidplanta.setText("Buscar");
        bidplanta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bidplantaActionPerformed(evt);
            }
        });

        jLabel3.setText("Id Actividad:");

        bidact.setText("Buscar");
        bidact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bidactActionPerformed(evt);
            }
        });

        jtabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtabla);

        bregresar.setText("Regresar");
        bregresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bregresarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tidplanta, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(15, 15, 15)
                                .addComponent(bidplanta)
                                .addGap(37, 37, 37)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tidact, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(bidact))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(140, 140, 140)
                                .addComponent(jLabel1))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(215, 215, 215)
                        .addComponent(bregresar)))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tidplanta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(bidplanta)
                    .addComponent(jLabel3)
                    .addComponent(tidact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bidact))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bregresar)
                .addContainerGap(58, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bidplantaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bidplantaActionPerformed

           Conexion con = new Conexion();
        try {
            Statement st =  con.conexionDB().createStatement();
            
           int idplanta = Integer.parseInt( tidplanta.getText());
        
            ResultSet rs = st.executeQuery("SELECT IdOrq AS IdOrquidea, IdAct AS IdActividad, "
                    + "TipoAct AS TipoActividad, FechaAct AS Fecha "
                    + "from tb_actividades  where IdOrq="+idplanta);
                ResultSetMetaData rsmd = rs.getMetaData();
                int numcol = rsmd.getColumnCount();
                
                DefaultTableModel model = new DefaultTableModel();
                this.jtabla.setModel(model);
            
                for(int x=1; x<= numcol; x++){
                model.addColumn(rsmd.getColumnLabel(x));
                    
                }
                
                while(rs.next()){
                Object [] fila = new Object[numcol];
                
                for(int y=0; y< numcol; y++){
                
                    fila[y] = rs.getObject(y+1);
                    
                    
                }
                model.addRow(fila);
                
                }
         
        } catch (SQLException ex) {
            Logger.getLogger(EstadoProd.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        // TODO add your handling code here:
    }//GEN-LAST:event_bidplantaActionPerformed

    private void bregresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bregresarActionPerformed

        this.setVisible(false);
        // TODO add your handling code here:
    }//GEN-LAST:event_bregresarActionPerformed

    private void bidactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bidactActionPerformed

        
          Conexion con = new Conexion();
        try {
            Statement st =  con.conexionDB().createStatement();
            
           int idactividad = Integer.parseInt( tidact.getText());
        
            ResultSet rs = st.executeQuery("SELECT IdAct AS IdActividad, IdOrq AS IdOrquidea, "
                    + "TipoAct AS TipoActividad, FechaAct AS Fecha "
                    + "from tb_actividades  where IdAct="+idactividad);
            
                ResultSetMetaData rsmd = rs.getMetaData();
                int numcol = rsmd.getColumnCount();
                
                DefaultTableModel model = new DefaultTableModel();
                this.jtabla.setModel(model);
            
                for(int x=1; x<= numcol; x++){
                model.addColumn(rsmd.getColumnLabel(x));
                    
                }
                
                while(rs.next()){
                Object [] fila = new Object[numcol];
                
                for(int y=0; y< numcol; y++){
                
                    fila[y] = rs.getObject(y+1);
                    
                    
                }
                model.addRow(fila);
                
                }
                
                
        
           
        } catch (SQLException ex) {
            Logger.getLogger(EstadoProd.class.getName()).log(Level.SEVERE, null, ex);
        }
      
        
    }//GEN-LAST:event_bidactActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Actividades.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Actividades.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Actividades.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Actividades.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Actividades().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bidact;
    private javax.swing.JButton bidplanta;
    private javax.swing.JButton bregresar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtabla;
    private javax.swing.JTextField tidact;
    private javax.swing.JTextField tidplanta;
    // End of variables declaration//GEN-END:variables
}
